package com.dd.restcontroller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.dd.model.User;

@RestController
public class UserRestController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(UserRestController.class);
	
	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	
	
	@GetMapping("/user/{id}")
	public User findByid(@PathVariable Long id){
		System.err.println("loadbalance movie 8062");
		return restTemplate.getForObject("http://microservice-provider-user/"+id, User.class);
	}
	
	@GetMapping("/loginstance")
	public void loginstance(){
		ServiceInstance serviceInstance = loadBalancerClient.choose("microservice-provider-user");
		
		UserRestController.LOGGER.info("{}:{}:{}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
		
	}
}
